<?php

namespace Controller;

use Handler\LobbyHandler;
use Handler\PushHandler;
use Handler\SFSHandler;
use Handler\TPHandler;
use Handler\ManagerHandler;
use Extension\Tool_Des;
use Config\Cfg;
use Extension\Tools;

class LobbyController extends BaseController {
	/**
	 * 获取大厅配置(HTTP)
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_LobbyInfo($clientId, $json) {
		$platform = $json->platform;
		$lobbyData = LobbyHandler::mb_GetInstance ()->mb_GetLobbyInfo ( $platform );
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $lobbyData ) );
		return;
	}
	/**
	 * 获取游戏配置(HTTP)
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_AppInfo($clientId, $json) {
		$appData = LobbyHandler::mb_GetInstance ()->mb_GetAppInfo ();
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $appData ) );
		return;
	}
	/**
	 * 获取其他配置(HTTP)
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_ConfigInfo($clientId, $json) {
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( array (
				'other' => '' 
		) ) );
		return;
	}
	/**
	 * 获取是否需要验证手机号(HTTP)
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetVerifyPhoneEnable($clientId, $json) {
		// $sfsData = SFSHandler::mb_GetInstance ()->mb_GetLobbySetting ();
		// if ($sfsData->state == false) {
		// $this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, $sfsData->msg );
		// return;
		// }
		$data = array (
				// 'phoneverify' => $sfsData->data [0]->phoneverify,
				'phoneverify' => "0",
				'idlename' => ( string ) LobbyHandler::mb_GetInstance ()->mb_GetIdleName () 
		);
		
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $data ) );
		return;
	}
	/**
	 * 获取登录开关
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetLoginEnable($clientId, $json) {
		$sfsData = SFSHandler::mb_GetInstance ()->mb_GetLobbySetting ();
		if ($sfsData->state == false) {
			$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, $sfsData->msg );
			return;
		}
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, $sfsData->data [0]->gamecontrol );
	}
	// /**
	// * 获取视频配置(HTTP)
	// *
	// * @param unknown $clientId
	// * @param unknown $json
	// */
	// public function mb_VideoInfo($clientId, $json) {
	// $videoData = LobbyHandler::mb_GetInstance ()->mb_GetVideoInfo ();
	// $this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $videoData ) );
	// }
	/**
	 * 游戏登录
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GameLogin($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$scene = $json->scene;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$game = LobbyHandler::mb_GetInstance ()->mb_GetAppInfoForScene ( $scene );
		$user->mb_EnterGame ( $game ['id'] );
		LobbyHandler::mb_GetInstance ()->mb_StatisticsOnlineUsers ( $game ['id'], $user->m_Name, true );
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 游戏登出
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GameLogout($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		$sfsData = TPHandler::mb_GetInstance ()->mb_QueryDataToTP ( $user->m_Name );
		if ($sfsData->state == false) {
			$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, $sfsData->msg );
			return;
		}
		$uData = json_decode ( $sfsData->data );
		$user->m_Coin = $uData->coin;
		
		$game = LobbyHandler::mb_GetInstance ()->mb_GetAppInfoForId ( $user->m_GameId );
		$user->mb_ExitGame ();
		LobbyHandler::mb_GetInstance ()->mb_StatisticsOnlineUsers ( $game ['id'], $user->m_Name, false );
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 记录游戏记录
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GameLog($clientId, $json) {
		return;
		$token = $json->token;
		$recNo = $json->recno;
		
		$gameScene = $json->gamescene;
		$before = $json->before;
		$coin = $json->coin;
		$later = $json->later;
		
		$appInfo = LobbyHandler::mb_GetInstance ()->mb_GetAppInfoForScene ( $gameScene );
		$gameId = $appInfo ['id'];
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		ManagerHandler::mb_GetInstance ()->mb_InsertGameLog ( $user->m_Id, $user->m_Name, $before, $coin, $later, $gameId );
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取数据记录
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetDataRecord($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$type = $json->type; // 0:充值记录，1:消费记录
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$sfsData = SFSHandler::mb_GetInstance ()->mb_QueryDataRecord ( $user->m_Name, $type );
		if ($sfsData->state == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, $sfsData->msg );
			return;
		}
		$data = array ();
		for($i = 0; $i < count ( $sfsData->data ); $i ++) {
			$tmpData = $sfsData->data [$i];
			if ($type == 0) {
				$tmp = array (
						'time' => date ( 'Y-m-d H:i:s', substr ( $tmpData->datetime, 0, - 3 ) ),
						'coins' => $tmpData->value,
						'remark' => $tmpData->type 
				);
			} else {
				$tmp = array (
						'time' => date ( 'Y-m-d H:i:s', substr ( $tmpData->datetime, 0, - 3 ) ),
						'coins' => $tmpData->coins,
						'remark' => $tmpData->gamename 
				);
			}
			
			$data [] = $tmp;
		}
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, json_encode ( $data ) );
	}
	/**
	 * 金币兑换
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_CoinsExchange($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$name = $json->name;
		$phone = $json->phone;
		$alipay = $json->alipay;
		$count = $json->count;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$sfsData = SFSHandler::mb_GetInstance ()->mb_CoinsExchange ( $user->m_Name, $name, $phone, $alipay, $count );
		if ($sfsData->state == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, $sfsData->msg );
			return;
		}
		
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '您的申请已提交，请注意查收' );
		$user->mb_SaveData ( - $count, '[大厅]金币兑换' );
		PushHandler::mb_GetInstance ()->mb_UserDataPush ( $user );
	}
	/**
	 * 绑定代理
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_BindingAgent($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$agent = $json->agent;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$sfsData = SFSHandler::mb_GetInstance ()->mb_BindingAgent ( $user->m_Name, $agent );
		if ($sfsData->state == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, $sfsData->msg );
			return;
		}
		
		$user->m_Agent = $agent;
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, '绑定成功' );
	}
	/**
	 * 获取游戏信息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetGameInfo($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		$gameId = $json->gameid;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		// $sfsData = SFSHandler::mb_GetInstance ()->mb_GetGameInfo ( $gameId );
		$sfsData = TPHandler::mb_GetInstance ()->mb_GetGameConfigToTP ( $gameId );
		if ($sfsData->state == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, $sfsData->msg );
			return;
		}
		// $name = $sfsData->data [0]->deskname;
		// $limitWin = $sfsData->data [0]->xianhong;
		// $miniBet = $sfsData->data [0]->minyafen;
		
		$gConfig = json_decode ( $sfsData->data );
		$name = $gConfig->name;
		$limitWin = $gConfig->limit;
		$name = "$name [限红 $limitWin ]";
		
		$gameRooms = array ();
		$gameRooms [] = array (
				'id' => 0,
				'name' => $name 
		);
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, json_encode ( $gameRooms ) );
	}
	/**
	 * 获取充值信息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetRechargeInfo($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$sfsData = SFSHandler::mb_GetInstance ()->mb_GetLobbySetting ();
		if ($sfsData->state == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, $sfsData->msg );
			return;
		}
		
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, $sfsData->data [0]->topuphelp );
	}
	/**
	 * 获取用户上一次登录游戏的状态
	 *
	 * @param unknown $clientId        	
	 * @param unknown $json        	
	 */
	public function mb_GetUserLastEnterGameRecord($clientId, $json) {
		$token = $json->token;
		$recNo = $json->recno;
		
		$user = $this->mb_CheckUserIsLogining ( $clientId, $recNo, $token );
		if ($user == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Distance, ErrMsg_Distance );
			return;
		}
		
		$sfsData = SFSHandler::mb_GetInstance ()->mb_UserLastEnterGameRecord ( $user->m_Name );
		if ($sfsData->state == false) {
			$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Toast, $sfsData->msg );
			return;
		}
		
		$zoneName = $sfsData->data [0]->scene;
		$roomName = $sfsData->data [0]->roomname;
		$roomNum = $sfsData->data [0]->roomnum;
		$dealer = $sfsData->data [0]->dealer;
		
		$data = array (
				'zonename' => $zoneName,
				'roomname' => $roomName,
				'dealer' => $dealer,
				'roomnum' => $roomNum 
		);
		$this->mb_OutTextMessage ( $clientId, $recNo, ErrCode_Normal, json_encode ( $data ) );
	}
}